package com.ccdt.tv.smartrecommend.view.widget.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ccdt.tv.smartrecommend.R;

/**
 * Created by sheng on 2018/9/17.
 * 直播 预约提示 dialog
 * 后期直播预约提示会已网页的形式弹出，这种方式基本废弃
 * 暂时保留未他用
 */

public class DialogView extends FrameLayout {

    public DialogView(@NonNull Context context) {
        super(context);
    }

    public static class Builder {
        private String title;
        private String message;
        private String confirmButtonText;
        private String cancelButtonText;
        private View.OnClickListener confirmButtonClickListener;
        private View.OnClickListener cancelButtonClickListener;
        private DialogView dialog;
        private View layout;

        public Builder(Context context) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new DialogView(context);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.layout_dialog, null);
            dialog.addView(layout);

            FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            params.width = 400;
            params.height = 300;
            params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
            layout.setLayoutParams(params);
//            DisplayMetrics dm = context.getResources().getDisplayMetrics();
//            mWidth = dm.widthPixels;
//            mHeight = dm.heightPixels;
//            mParams = layout.findViewById(R.id.rl_root).getLayoutParams();//获取布局参数
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setConfirmButton(String confirmButtonText, View.OnClickListener listener) {
            this.confirmButtonText = confirmButtonText;
            this.confirmButtonClickListener = listener;
            return this;
        }

        public Builder setCancelButton(String cancelButtonText, View.OnClickListener listener) {
            this.cancelButtonText = cancelButtonText;
            this.cancelButtonClickListener = listener;
            return this;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private void create() {
            if (!TextUtils.isEmpty(title)) {
                ((TextView) layout.findViewById(R.id.tv_title)).setText(title);
            }
            if (message != null) {      //设置提示内容
                ((TextView) layout.findViewById(R.id.tv_message)).setText(message);
            }
//            dialog.setContentView(layout);
//            dialog.setCancelable(true);     //用户可以点击手机Back键取消对话框显示
//            dialog.setCanceledOnTouchOutside(false);        //用户不能通过点击对话框之外的地方取消对话框显示
        }

        /**
         * 创建双按钮对话框
         *
         * @return
         */
        public DialogView createTwoButtonDialog() {
            layout.findViewById(R.id.ll_btn).setVisibility(View.VISIBLE);
//            Log.w("dialog", "createTwoButtonDialog: " + layout.findViewById(R.id.btn_confirm).requestFocus());
//            Log.w("dialog", "createTwoButtonDialog: " + layout.findViewById(R.id.btn_cancel).requestFocus());
            layout.findViewById(R.id.btn_confirm).setOnClickListener(confirmButtonClickListener);
            layout.findViewById(R.id.btn_cancel).setOnClickListener(cancelButtonClickListener);
            //如果传入的按钮文字为空，则使用默认的“是”和“否”
            if (!TextUtils.isEmpty(confirmButtonText)) {
                ((Button) layout.findViewById(R.id.btn_confirm)).setText(confirmButtonText);
            } else {
                ((Button) layout.findViewById(R.id.btn_confirm)).setText("是");
            }
            if (!TextUtils.isEmpty(cancelButtonText)) {
                ((Button) layout.findViewById(R.id.btn_cancel)).setText(cancelButtonText);
            } else {
                ((Button) layout.findViewById(R.id.btn_cancel)).setText("否");
            }
            create();
            return dialog;
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.w("DialogView", "dispatchKeyEvent: " + event);
        return super.dispatchKeyEvent(event);
    }
}
